// ActionScript file
import com.controls.TimeStrip;
import com.controls.SessionInitPopup;
import com.fs.FSVideoDisplay;
import com.util.DateTimeUtils;
import com.fileio.FileDownload;
import mx.controls.Alert;
import mx.core.Application;
import mx.core.IFlexDisplayObject;
import mx.effects.easing.*
import mx.events.VideoEvent;
import mx.events.SliderEvent;
import flash.display.*
import mx.managers.PopUpManager;
import mx.rpc.events.ResultEvent;
import VideoWindow;
import mx.controls.ProgressBar;

/*
//------------------------------
//  General Variables
//------------------------------
*/
[Bindable] private var newUrl:String;
public var Player:VideoExample;	
public var window: VideoWindow;		

[Bindable]
public var _username:String;

[Bindable]
public var _password:String;

//login popup box
public var _loginPopup:IFlexDisplayObject;

[Bindable]
public var _downloadProg:ProgressBar = downloadProg;

/*
//------------------------------
//  Download Variables
//------------------------------
*/
[Bindable]
public var _downloadURL:String;
			
/*
//------------------------------
//  Search Variables
//------------------------------
*/

[Bindable] private var search_tags:String;
[Bindable] private var search_pagenumber:String;
[Bindable] private var searchResult_url:String;


/*
//------------------------------
//  Initializing Functions
//------------------------------
*/
private function initApp():void
{
	//this.homeWindow.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
	application.setStyle("backgroundColor","");
	application.setStyle("backgroundImage","");
}

//function called when login is successful
private function initHome():void
{
	application.setStyle("backgroundColor","#000000");
	application.label = 'Apollo FlexTube';
	_authenticationPopup = new SessionInitPopup();
    PopUpManager.addPopUp(_authenticationPopup, DisplayObject(Application.application), true);
    PopUpManager.centerPopUp(_authenticationPopup);
    featuredVideos.addEventListener(ResultEvent.RESULT,SessionInitListener);
	featuredVideos.send();
}
/*
//------------------------------
//  Login Functions
//------------------------------
*/




public function processLogin():void
{
	_username = vUsername.text;
	_password = vPassword.text;
	loginRequest.send();
	//create a new popup while the request is being sent
	_loginPopup = new LoginPopup;
	PopUpManager.addPopUp(_loginPopup, DisplayObject(Application.application), true);
	PopUpManager.centerPopUp(_loginPopup);
}

public function loginRequest_Result(evt:ResultEvent):void
{
	var check:String;
	//close the popup
	PopUpManager.removePopUp(_loginPopup);
	//Alert.show(evt.result.loginResult); 
	if(evt.result.loginResult == "yes")
	{
		check = "true";
		currentState = 'new_ui';
		initHome();
	}
	if(evt.result.loginResult == "no")
	{
		check = "true";
		Alert.show("Invalid username/password");
	}
	if(check != "true")
	{
		Alert.show(""+evt.result.loginResult);
	}
}

private function checkCookieResult(evt:ResultEvent):void
{
	if(evt.result.cookieResult == "success")
	{
		currentState = "new_ui";
		initHome();
	} 
}

/*
//------------------------------
//  Download Functions
//------------------------------
*/

public function downloadVideo(pb:ProgressBar):void
{
	com.fileio.FileDownload.startDownload(_downloadURL,pb);
}
/*
//------------------------------
//  Window/Stage Functions
//------------------------------
*/

/**
 * Responds to a window close event after the window's close button has been clicked.
 */                 
private function onClose(evt:MouseEvent):void
{
    window.closeSelf()
}

/**
 * Responds to a window minimize event, after the minimize button has been clicked
 */             
private function onMinimize(evt:MouseEvent):void
{
    //
}  
/**
 * Responds to a mouseDown event by starting an operating-system move sequence.
 */
private function onMouseDown(evt:MouseEvent):void
{
	application.stage.window.startMove();
}

/*
//------------------------------
//  Video Functions
//------------------------------
*/

private function playheadUpdate(evt:VideoEvent):void
{
	
}
public function changeURL(evt:ResultEvent):void
{		
	vPlayer.source = evt.result.file;	
	_downloadURL = evt.result.file;
	vPlayer.play();
	vPlayer.maintainAspectRatio = true;
	vPlayer.addEventListener(ProgressEvent.PROGRESS,onVideoLoaded);
	vPlayer.addEventListener(VideoEvent.PLAYHEAD_UPDATE,playheadUpdate);

}

private function switchScreen():void
{

  vPlayer.displayState = StageDisplayState.FULL_SCREEN;

}

private function updatePlayheadTime(t:int):void
   {
   vPlayer.playheadTime = t;
   }

public function changeVideo(url:String):void
{
	searchResult_url = url;
	formatUrl_Search.send();
}

public  function onVideoLoaded(evt:Event):void
{
     //  vProgress.maximum = vPlayer.totalTime;
}

private function prepareSearch():void
{
	search_tags = vSearch.text;
	if(search_pagenumber == null)
	{
		search_pagenumber = "1";
	}
	searchYouTube.send();
}

private function searchResults(evt:ResultEvent):void
{
	//Alert.show();
	if(evt.result.ut_response.video_list.total == "")
	{
		Alert.show("No results found! Try some different keywords");
	}
}
   
  private function str_replace( search:String, replace:String, original:String ):String
{
	var array:Array = original.split(search);
	return array.join(replace);
}
private function changeStatus(evt:Event):void
{
	if(vPlayer.playing == true)
	{
		vPlayer.pause();		
		[Embed(source="media/control_play.png")]
		var status_icon:Class;// = "@Embed('media/control_pause.png')";			
		//status_icon = [Embed(source='/media/control_play.png')];
		//Alert.show(status_icon);
	}else{
		vPlayer.play();
		//vStatus.label = "Pause";
	}
}